package batch.import_user;

import java.sql.*;
import batch.common.db.*;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.*;

public class remakeIdnPw
{
    private final String TABLE_NAME = "G_GBR_IMPORT_TEMP";
    private final String SPECIAL_ID = "ecircle";

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.remakeIdnPw").info("( * ) Start.");

        remakeIdnPw obj = new remakeIdnPw();
        obj.init();

        Logger.getLogger("import_user.remakeIdnPw").info("( * ) End.");
    }

    public void init() {
        Connection conn   = null;
        Statement  stmt   = null;
        ResultSet  rs     = null;

        String strTempPasswd = "";
        String strTempUserId = "";
        String strTempNewId  = "";
        String strUserId     = "";

        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT SEQ                                 \n")
                             .append("     , USERID                              \n")
                             .append("     , PASSWD                              \n")
                             .append("     , FNAME                               \n")
                             .append("     , LNAME                               \n")
                             .append("     , EMAIL                               \n")
                             .append("     , BIRTHDAY                            \n")
                             .append("     , GUBN                                \n")
                             .append("  FROM " + TABLE_NAME + "                  \n")

                             .append(" WHERE GUBN IN ('7', '97 id', '97 passwd', '98 id') \n");

        int    i = 0;

        String sSeq    = "";
        String sUserId = "";
        String sPasswd = "";
        String sFname  = "";
        String sLname  = "";
        String sEmail  = "";
        String sBirth  = "";
        String sGubn   = "";

        String[] arrTemp = null;

        try{
            checkManager chkMgr = new checkManager();

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(false);
            stmt= conn.createStatement();
            rs = stmt.executeQuery(sQuery.toString());

            QueryRunner qRunner = new QueryRunner();

            while (rs.next()) {
                i++;
                arrTemp = null;

                sSeq    = ObjectUtils.toString(rs.getObject(1));
                sUserId = ObjectUtils.toString(rs.getObject(2));
                sPasswd = ObjectUtils.toString(rs.getObject(3));
                sFname  = ObjectUtils.toString(rs.getObject(4));
                sLname  = ObjectUtils.toString(rs.getObject(5));
                sEmail  = ObjectUtils.toString(rs.getObject(6));
                sBirth  = ObjectUtils.toString(rs.getObject(7));
                sGubn   = ObjectUtils.toString(rs.getObject(8));

                //if Duplicate Id Error..
                if(sGubn.equals("7")){
                    strUserId = chkMgr.removeCharacters(sLname.toLowerCase());
                    strUserId = chkMgr.getNewUserId(conn, strUserId, TABLE_NAME);

                    sQuery.setLength(0);
                    sQuery.append("UPDATE " + TABLE_NAME + "           \n")
                          .append("   SET USERID = '" + strUserId + "' \n")
                          .append("     , REMAKE = 'RI'                \n")
                          .append(" WHERE SEQ    = '" + sSeq + "'      \n");

                }
                //if Id Format Error..
                else if(sGubn.equals("97 id") || sGubn.equals("98 id")){
                    //strTempPasswd = makeRandomData.makeDataUsIntSpChr("pw", 6, "F");
                    strTempUserId  = chkMgr.removeCharacters(sLname.toLowerCase());

                    if(sUserId.length() > 16){
                        if(StringUtils.indexOf(strTempUserId, ' ') > -1){
                            arrTemp = StringUtils.split(strTempUserId, " ");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempUserId, '-') > -1){
                            arrTemp = StringUtils.split(strTempUserId, "-");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempUserId, '/') > -1){
                            arrTemp = StringUtils.split(strTempUserId, "/");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempUserId, '(') > -1){
                            arrTemp = StringUtils.split(strTempUserId, "(");
                        }

                        if(arrTemp == null){
                            strTempNewId = chkMgr.removeCharacters(sFname.toLowerCase());
                        }
                        else{
                            strTempNewId = chkMgr.removeCharacters(arrTemp[0].toLowerCase());
                        }
                    }
                    else{
                        strTempNewId = chkMgr.removeCharacters(sFname.toLowerCase());
                    }

                    strTempNewId = chkMgr.getNewUserId(conn, strTempNewId, TABLE_NAME);

                    if(strTempNewId.length() > 16){
                        if(StringUtils.indexOf(strTempNewId, ' ') > -1){
                            arrTemp = StringUtils.split(strTempNewId, " ");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempNewId, '-') > -1){
                            arrTemp = StringUtils.split(strTempNewId, "-");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempNewId, '/') > -1){
                            arrTemp = StringUtils.split(strTempNewId, "/");
                        }

                        if(arrTemp == null && StringUtils.indexOf(strTempNewId, '(') > -1){
                            arrTemp = StringUtils.split(strTempNewId, "(");
                        }

                        if(arrTemp == null){
                            arrTemp = StringUtils.split(sEmail, "@");
                            strTempNewId = chkMgr.removeCharacters(arrTemp[0].toLowerCase());
                        }
                        else{
                            strTempNewId = chkMgr.removeCharacters(arrTemp[0].toLowerCase());
                        }
                    }
                    else if(strTempNewId.length() < 3){
                        arrTemp = StringUtils.split(sEmail, "@");
                        strTempNewId = chkMgr.removeCharacters(arrTemp[0].toLowerCase());
                    }

                    strTempNewId = chkMgr.getNewUserId(conn, strTempNewId, TABLE_NAME);

                    if(strTempNewId.length() > 16 || strTempNewId.length() < 3){
                        strTempNewId = makeRandomData.makeDataUsIntSpChr(SPECIAL_ID, 4, "F");
                        strTempNewId = chkMgr.getNewUserIdForRandom(conn, strTempNewId, TABLE_NAME, SPECIAL_ID);
                    }

                    sQuery.setLength(0);
                    sQuery.append("UPDATE " + TABLE_NAME + "                \n")
                          .append("   SET USERID = '" + strTempNewId + "'   \n")
                          .append("     , REMAKE = 'RI'                     \n")
                          .append(" WHERE SEQ    = '" + sSeq + "'           \n");

                }
                //if Password Format Error..
                else if(sGubn.equals("97 passwd")){
                    //strTempPasswd = makeRandomData.makeDataUsIntSpChr("pw", 6, "F");

                    if(sBirth.length() == 8 && NumberUtils.isDigits(sBirth)){
                        strTempPasswd = "pw" + sBirth.substring(6, 8)
                                             + sBirth.substring(4, 6)
                                             + sBirth.substring(0, 4);
                    }
                    else{
                        strTempPasswd = "pw";
                    }

                    sQuery.setLength(0);
                    sQuery.append("UPDATE " + TABLE_NAME + "               \n")
                          .append("   SET PASSWD = '" + strTempPasswd + "' \n")
                          .append("     , REMAKE = 'RP'                    \n")
                          .append(" WHERE SEQ    = '" + sSeq + "'          \n");
                }

                qRunner.update(conn, sQuery.toString());

                if(i % 100 == 0){
                    Logger.getLogger("import_user.remakeIdnPw").info("here we are == " + i);
                }
            }

            conn.commit();
        }
        catch(Exception ex){
            if(conn != null) try { conn.rollback(); } catch (Exception se) {}

            ex.printStackTrace();
            Logger.getLogger("import_user.remakeIdnPw").info(ex.toString());
        }
        finally{
            if (rs  != null)  try { rs.close();   } catch(Exception se) {}
            if (stmt != null) try { stmt.close(); } catch(Exception se) {}
            if (conn != null) try { conn.close(); } catch(Exception se) {}
        }
    }
}
